﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Models;
using Yuebon.Commons.Tree;
using Yuebon.Shop.Application;
using Yuebon.Shop.Dtos;
using Yuebon.Shop.IServices;
using Yuebon.Shop.Models;
using Yuebon.WebApp.Controllers;
using Yuebon.WebApp.Models;

namespace Yuebon.WebApp.Areas.Shop.Controllers
{
    [Area("Shop")]
    [Route("Shop/[controller]/[action]")]
    public class ReferralsController :  BusinessController<Referrals, IReferralsService>
    {
        public ReferralsController(IReferralsService _iService) : base(_iService)
        {
            iService = _iService;

            AuthorizeKey.InsertKey = "Referrals/Add";
            AuthorizeKey.UpdateKey = "Referrals/Edit";
            AuthorizeKey.DeleteKey = "Referrals/Delete";
            AuthorizeKey.UpdateEnableKey = "Referrals/Enable";
            AuthorizeKey.ListKey = "Referrals/List";
            AuthorizeKey.ViewKey = "Referrals/View";
        }

        protected override void OnBeforeInsert(Referrals info)
        {
            //留给子类对参数对象进行修改
            info.Id = GuidUtils.CreateNo();
            info.ReferralStatus = 1;
            info.RequetDate = info.AuditDate = DateTime.Now;
            info.ReferralsURL = "http://www.zhantucar.com/#/?shopid=" + info.Id;
        }

        /// <summary>
        /// 修改会员前对字段的默认处理
        /// </summary>
        /// <returns>指定对象的集合</returns>
        protected override void OnBeforeUpdate(Referrals info)
        {
            if (info.IsRepeled == true)
            {
                info.ReferralStatus = 0;
            }
            else
            {
                info.ReferralStatus = 1;
            }
        }

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <returns>指定对象的集合</returns>
        [HttpGet]
        public override IActionResult FindWithPager()
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            CheckAuthorized(AuthorizeKey.ListKey);
            string keywords = Request.Query["search"].ToString() == null ? "" : Request.Query["search"].ToString();
            string orderByDir = Request.Query["order"].ToString() == null ? "" : Request.Query["order"].ToString();
            string orderFlied = Request.Query["sort"].ToString() == "" ? "Id" : Request.Query["sort"].ToString();
            string where = GetPagerCondition();
            bool order = orderByDir == "asc" ? false : true;
            if (!string.IsNullOrEmpty(keywords))
            {
                where += string.Format(" and (UserName like '%{0}%' or RealName like '%{0}%' or NickName like '%{0}%' or IdentityCard like '%{0}%')", keywords);
            }
            PagerInfo pagerInfo = GetPagerInfo();
            ReferralsApp membersApp = new ReferralsApp();
            List<ReferralsDto> list = membersApp.FindWithPager(where, pagerInfo, orderFlied, order);

            //构造成Json的格式传递
            var result = new
            {
                total = pagerInfo.RecordCount,
                rows = list
            };
            return ToJsonContent(result);
        }

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <returns>指定对象的集合</returns>
        [HttpGet]
        public IActionResult GetAll()
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            CheckAuthorized(AuthorizeKey.ListKey);
            ReferralsApp referralsApp = new ReferralsApp();
            IEnumerable<ReferralsDto> list = referralsApp.GetAll();
            var treeList = new List<TreeSelectModel>();
            foreach (ReferralsDto item in list)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.MemberId;
                treeModel.text = item.MemberName+"/"+item.ShopName;
                treeModel.parentId = "";
                treeList.Add(treeModel);
            }
            return ToJsonContent(treeList.TreeSelectJson());
        }
    }
}